#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import cv2
import numpy as np
import pytesseract
import sys
from matplotlib import pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

import logging
import json
from pydash import py_


def setup_custom_logger(name):
    formatter = logging.Formatter(fmt='%(asctime)s %(levelname)-8s %(message)s',
                                  datefmt='%Y-%m-%d %H:%M:%S')
    # handler = logging.FileHandler('log.txt', mode='w')
    # handler.setFormatter(formatter)
    screen_handler = logging.StreamHandler(stream=sys.stdout)
    screen_handler.setFormatter(formatter)
    logger = logging.getLogger(name)
    logger.setLevel(logging.DEBUG)
    # logger.addHandler(handler)
    logger.addHandler(screen_handler)
    return logger


logger = setup_custom_logger('app')

pytesseract.pytesseract.tesseract_cmd = "/usr/bin/tesseract"


#
# card_champion = cv2.cvtColor(cv2.imread('/home/liusen/IdeaWorkspace/wechat-jump/mijing/card_champion.png'),
#                              cv2.COLOR_BGR2GRAY)
# daily_task = cv2.cvtColor(cv2.imread('/home/liusen/IdeaWorkspace/wechat-jump/mijing/daily_task.png'),
#                           cv2.COLOR_BGR2GRAY)
# main_page = cv2.cvtColor(cv2.imread('/home/liusen/IdeaWorkspace/wechat-jump/mijing/main_page.png'),
#                          cv2.COLOR_BGR2GRAY)
#
#

# def getContentMainPage(img):
#     img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
#     img = img[382:403, 417:467]
#     frame_threshed = cv2.inRange(img, 0, 255)
#     img = cv2.bitwise_or(img, img, mask=frame_threshed)
#     # plt.imshow(img), plt.show()
#     template = main_page
#     # plt.imshow(img), plt.show()
#     # plt.imshow(template), plt.show()
#     # cv2.imwrite('/home/liusen/IdeaWorkspace/wechat-jump/mijing/main_page.png', img)
#     res = cv2.matchTemplate(img, template, cv2.TM_CCOEFF_NORMED)
#     loc = np.where(res >= 0.8)
#     if len(list(zip(*loc[::-1]))) >= 1:
#         logger.debug("main_page match value : {}".format(res[0][0]))
#         return True
#     else:
#         return False


def test():
    # path = '/home/liusen/IdeaWorkspace/wechat-jump/mijing/00001.png'
    # path = '/home/liusen/IdeaWorkspace/wechat-jump/mijing/00010.png'
    # path = '/home/liusen/IdeaWorkspace/wechat-jump/mijing/00011.png'
    # path = '/home/liusen/IdeaWorkspace/wechat-jump/mijing/00012.png'
    # path = '/home/liusen/IdeaWorkspace/wechat-jump/mijing/00013.png'
    # path = '/home/liusen/IdeaWorkspace/wechat-jump/mijing/00014.png'
    # path = '/home/liusen/IdeaWorkspace/wechat-jump/mijing/00016.png'
    # path = '/home/liusen/IdeaWorkspace/wechat-jump/mijing/00017.png'
    # path = '/home/liusen/IdeaWorkspace/wechat-jump/mijing/00018.png'
    # path = '/home/liusen/IdeaWorkspace/wechat-jump/mijing/00019.png'
    # path = '/home/liusen/IdeaWorkspace/wechat-jump/mijing/00020.png'
    # path = '/home/liusen/IdeaWorkspace/wechat-jump/mijing/00026.png'
    # path = '/home/liusen/IdeaWorkspace/wechat-jump/mijing/00027.png'

    path = '/home/liusen/IdeaWorkspace/wechat-jump/mijing/201809-special-选牌.png'

    img = cv2.imread(path)
    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
    # plt.imshow(img)
    # plt.show()

    # img = img[220:228, 584:598]  # 确定按钮
    # img = img[652:677, 167:185] # 回合数
    # cv2.imwrite('/home/liusen/IdeaWorkspace/wechat-jump/mijing/确认.png', img)
    # hsv = cv2.cvtColor(img, cv2.COLOR_RGB2HSV)
    #
    # FILTER_MIN = np.array([0, 0, 0], np.uint8)
    # FILTER_MAX = np.array([250, 50, 100], np.uint8)
    # frame_threshed = cv2.inRange(img, FILTER_MIN, FILTER_MAX)
    # # frame_threshed2 = cv2.cvtColor(frame_threshed, cv2.COLOR_HSV2BGR)
    # img = cv2.bitwise_or(img, img, mask=frame_threshed)
    # img[np.where((hsv == [0, 0, 0]).all(axis=2))] = [255, 255, 255]

    # grey = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
    plt.subplot(221), plt.imshow(img)
    # hist = cv2.calcHist([grey], [0], None, [256], [0, 256])
    # plt.subplot(222), plt.plot(hist), plt.xlim([0, 256])
    # eq_grey = cv2.equalizeHist(grey)
    # plt.subplot(223), plt.imshow(eq_grey, cmap=plt.cm.gray)
    # hist = cv2.calcHist([eq_grey], [0], None, [256], [0, 256])
    # plt.subplot(224), plt.plot(hist), plt.xlim([0, 256])

    # ret, th1 = cv2.threshold(grey, 150, 255, cv2.THRESH_BINARY)
    # plt.subplot(222), plt.imshow(th1, cmap=plt.cm.gray)
    # plt.subplot(223), plt.imshow(cv2.adaptiveThreshold(grey, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 11, 2),
    #                              cmap=plt.cm.gray)
    # plt.subplot(224), plt.imshow(
    #     cv2.adaptiveThreshold(grey, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 11, 2), cmap=plt.cm.gray)

    # plt.subplot(222), plt.imshow(hsv[:, :, 0], cmap=plt.cm.gray)
    # plt.subplot(223), plt.imshow(hsv[:, :, 1], cmap=plt.cm.gray)
    # plt.subplot(224), plt.imshow(hsv[:, :, 2], cmap=plt.cm.gray)

    # h1 = cv2.calcHist([hsv], [0], None, [256], [0, 256])
    # h2 = cv2.calcHist([hsv], [1], None, [256], [0, 256])
    # h3 = cv2.calcHist([hsv], [2], None, [256], [0, 256])
    # plt.subplot(222), plt.plot(h1, color='b'), plt.xlim([0, 256])
    # plt.subplot(223), plt.plot(h2, color='b'), plt.xlim([0, 256])
    # plt.subplot(224), plt.plot(h3, color='b'), plt.xlim([0, 256])

    # print(pytesseract.image_to_string(th1, lang='chi_sim'))

    # plt.imshow(img)
    plt.show()


def getFilteredImg(img):
    FILTER_MIN = np.array([0, 0, 0], np.uint8)
    FILTER_MAX = np.array([70, 70, 70], np.uint8)
    frame_threshed = cv2.inRange(img, FILTER_MIN, FILTER_MAX)
    # frame_threshed2 = cv2.cvtColor(frame_threshed, cv2.COLOR_HSV2BGR)
    result = cv2.bitwise_or(img, img, mask=frame_threshed)
    return result


if __name__ == '__main__':

    sys.exit(0)
